package com.wnxy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wnxy.dto.ManagerRoleDto;
import com.wnxy.dto.PageRoleDto;
import com.wnxy.dto.RoleDto;
import com.wnxy.dto.StatusCode;
import com.wnxy.exception.UpdateException;
import com.wnxy.model.Manager;
import com.wnxy.model.ManagerRole;
import com.wnxy.mapper.ManagerRoleMapper;
import com.wnxy.service.ManagerRoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wnxy.service.ManagerService;
import com.wnxy.util.BeanCopyUtil;
import com.wnxy.vo.EmPowerVo;
import com.wnxy.vo.RoleVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author alice
 * @since 2021-11-12
 */
@Service
public class ManagerRoleServiceImpl extends ServiceImpl<ManagerRoleMapper, ManagerRole> implements ManagerRoleService {
    @Autowired
    private ManagerService managerService;

    @Override
    public PageRoleDto findRole(RoleVo roleVo) {
        //名字关键字模糊查询
        QueryWrapper<ManagerRole> qw = new QueryWrapper<>();
//        if(roleVo.getRoleId()!=null) qw.eq("role_id",roleVo.getRoleId());
        Page<ManagerRoleDto> page = getBaseMapper().findByrole(new Page<Manager>(roleVo.getCurrent(), roleVo.getSize()), roleVo.getRoleId());
        List<ManagerRoleDto> records = page.getRecords();
        PageRoleDto pageRoleDto = BeanCopyUtil.copyProperties(page, PageRoleDto.class);
//        pageRoleDto.setRecords(new ArrayList<ManagerRoleDto>(map.values()));
        return pageRoleDto;
    }

    @Override
    public void empower(EmPowerVo emPowerVo) {
        List<Integer> roleIds = emPowerVo.getRoleIds();
        //删除原有关系
        remove(new QueryWrapper<ManagerRole>().eq("manager_id", emPowerVo.getManagerId()));
        if(emPowerVo.getRoleIds()!=null&&emPowerVo.getRoleIds().size()>0){
            List<ManagerRole> managerRoles = new ArrayList<>();
            roleIds.forEach( roleId->{
                ManagerRole managerRole = new ManagerRole();
                managerRole.setRoleId(roleId);
                managerRole.setManagerId(emPowerVo.getManagerId());
                managerRoles.add(managerRole);
            });
            boolean b = saveBatch(managerRoles);
            if(!b) throw new UpdateException(StatusCode.ERROR.getCode(), "权限修改错误");
        }else {
            ManagerRole managerRole = new ManagerRole();
            managerRole.setRoleId(5);
            managerRole.setManagerId(emPowerVo.getManagerId());
            save(managerRole);
        }

    }

    @Override
    public List<RoleDto> lodeManagerRole() {

        List<ManagerRole> list = list();

        return BeanCopyUtil.copyList(list,RoleDto.class);
    }
}
